@use 'sass:map';

@use './shared' as *;
@use './design' as *;

$pagination: () !default;
$pagination: map.merge(
  (
    color: inherit,
    color-hover: get-css-var('color-primary-base'),
    color-focus: get-css-var('pagination-color-hover'),
    color-disabled: get-css-var('content-color-disabled'),
    color-active: get-css-var('pagination-color-hover'),
    bg-color: get-css-var('fill-color-background'),
    bg-color-hover: get-css-var('color-primary-opacity-8'),
    bg-color-focus: get-css-var('pagination-bg-color-hover'),
    bg-color-disabled: get-css-var('pagination-bg-color'),
    bg-color-active: get-css-var('color-primary-base'),
    b-color: get-css-var('border-color-base'),
    b-color-hover: get-css-var('color-primary-base'),
    b-color-focus: get-css-var('pagination-b-color-hover'),
    b-color-disabled: get-css-var('fill-color-disabled'),
    b-color-active: get-css-var('pagination-b-color-hover'),
    s-color-focus: get-css-var('color-primary-opacity-6'),
    border: get-css-var('border-shape') get-css-var('pagination-b-color'),
    radius: get-css-var('radius-base'),
    size: 32px,
    item-span: 6px,
    plugin-span: 10px
  ),
  $pagination
);

.#{$namespace}-pagination {
  &-vars {
    @include define-preset-values('pagination', $pagination);
  }

  @mixin define-pagination-style($style-map) {
    @include define-preset-style('pagination', $style-map);
  }

  @include basis;

  display: inline-flex;
  align-items: center;

  &--small {
    @include define-css-var('pagination-size', 28px);
  }

  &--large {
    @include define-css-var('pagination-size', 36px);
  }

  &__list {
    display: flex;
    align-items: center;
    padding: 0;
    margin: 0;
  }

  $item: #{&}__item;

  @mixin item-disabled {
    color: get-css-var('pagination-color-disabled');
    cursor: not-allowed;
    border-color: get-css-var('pagination-b-color-disabled');
    box-shadow: none;
  }

  &__item {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: get-css-var('pagination-size');
    height: get-css-var('pagination-size');
    padding: 0 6px;
    margin-inline-end: get-css-var('pagination-item-span');
    list-style: none;
    cursor: pointer;
    user-select: none;
    border: get-css-var('pagination-border');
    border-radius: get-css-var('pagination-radius');
    outline: 0;
    transition: get-css-var('transition-color'), get-css-var('transition-background'),
      get-css-var('transition-border'), get-css-var('transition-shadow');

    &:last-of-type {
      margin-inline-start: 0;
    }

    &:hover {
      color: get-css-var('pagination-color-hover');
      border-color: get-css-var('pagination-b-color-hover');
    }

    &:focus,
    &:focus-within {
      color: get-css-var('pagination-color-focus');
      border-color: get-css-var('pagination-b-color-focus');
      box-shadow: get-css-var('shadow-focus') get-css-var('pagination-s-color-focus');
    }

    &--active {
      color: get-css-var('pagination-color-active');
      border-color: get-css-var('pagination-b-color-active');
    }

    &--disabled {
      &,
      &:hover,
      &:focus,
      &:focus-within,
      &#{$item}--active {
        @include item-disabled;
      }

      &#{$item}--active {
        background-color: get-css-var('pagination-bg-color-disabled');
      }
    }
  }

  &--background &__item {
    background-color: get-css-var('pagination-bg-color');

    &:hover {
      background-color: get-css-var('pagination-bg-color-hover');
    }

    &:focus,
    &:focus-within {
      background-color: get-css-var('pagination-bg-color-focus');
    }

    &--active {
      @include define-pagination-style(
        (
          color-active: 'color-white',
          color-hover: 'color-white',
          color-focus: 'color-white',
          bg-color-disabled: 'fill-color-disabled'
        )
      );

      &,
      &:hover,
      &:focus {
        background-color: get-css-var('pagination-bg-color-active');
      }
    }

    &--disabled {
      &,
      &:hover,
      &:focus,
      &:focus-within {
        background-color: get-css-var('pagination-bg-color-disabled');
      }
    }
  }

  &--no-border &__item {
    border: 0;
  }

  &__jump,
  &__size,
  &__total {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  &__jump {
    white-space: nowrap;
    user-select: none;

    &-input {
      min-width: 60px;
      height: get-css-var('pagination-size');
      margin: 0 4px;
      user-select: auto;
    }
  }

  &__size {
    min-width: 106px;

    &-select {
      width: 100%;

      .#{$namespace}-select__selector {
        height: get-css-var('pagination-size');
      }
    }
  }

  &__total {
    white-space: nowrap;
  }

  & > &__jump,
  & > &__size,
  & > &__total {
    margin-inline-start: get-css-var('pagination-plugin-span');

    &--prefix {
      margin-inline: 0 get-css-var('pagination-plugin-span');
    }
  }
}
